package com.smartwebee.android.blespp.hospital;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.google.gson.Gson;
import com.smartwebee.android.blespp.R;
import com.smartwebee.android.blespp.databinding.ActivityMyTestsBinding;
import com.smartwebee.android.blespp.hospital.fragment.SelfTestFragment;
import com.smartwebee.android.blespp.hospital.selftest.SelfTestActivity;
import com.smartwebee.android.blespp.http.HttpRequest;
import com.smartwebee.android.blespp.http.RequestInterface;
import com.smartwebee.android.blespp.http.VolleyReqQueue;
import com.smartwebee.android.blespp.resp.ScoreResp;
import com.smartwebee.android.blespp.utils.UIUtils;
import com.smartwebee.android.blespp.utils.Utils;
import com.taobao.accs.utl.UT;

import java.util.HashMap;
import java.util.Map;

public class MyTestsActivity extends BaseActivity implements RequestInterface {

    ActivityMyTestsBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_my_tests);
        TAG = "MyTestsActivity";
        requestQueue = VolleyReqQueue.getInstance(this).getRequestQueue();
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        httpFindScoreList();
    }

    private void httpFindScoreList() {
        showLoading();
        HttpRequest httpRequest = new HttpRequest(Request.Method.POST, Utils.findUserScoreList,
                this) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("sn", Utils.registerResp.getData().getSn());
                return map;
            }
        };
        requestQueue.add(httpRequest);
    }

    private void initView() {
        binding.btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        binding.clayoutToChart1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toSelfTestActivity(SelfTestFragment.TYPE1);
            }
        });
        binding.clayoutToChart2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toSelfTestActivity(SelfTestFragment.TYPE2);
            }
        });
        binding.clayoutToChart3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toSelfTestActivity(SelfTestFragment.TYPE3);
            }
        });
        binding.clayoutToChart4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toSelfTestActivity(SelfTestFragment.TYPE4);
            }
        });
    }

    public void toSelfTestActivity(String title) {
        Intent intent = new Intent(this, ScoreDetailActivity.class);
        intent.putExtra(SelfTestFragment.INTENT_TITLE, title);
        startActivity(intent);
    }

    @Override
    public void onSuccess(String id, String resp) {
        dismissLoading();
        ScoreResp scoreResp = new Gson().fromJson(resp, ScoreResp.class);
        if (Utils.parseData(scoreResp)) {
            for (int i = 0; i < scoreResp.getData().size(); i++) {
                ScoreResp.DataBean bean = scoreResp.getData().get(i);
                if ("1".equals(bean.getSid())) {
                    binding.tvScore1.setText(bean.getScore());
                } else if ("2".equals(bean.getSid())) {
                    binding.tvScore2.setText(bean.getScore());
                } else if ("3".equals(bean.getSid())) {
                    binding.tvScore3.setText(bean.getScore());
                } else if ("4".equals(bean.getSid())) {
                    binding.tvScore4.setText(bean.getScore());
                }
            }
        }
    }

    @Override
    public void onError(String error) {
        dismissLoading();
        UIUtils.showTips(this, error);
    }
}
